package com.ax.base.drugadministration.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.drugadministration.dao.BaseDrugDepMapper;
import com.ax.base.drugadministration.domain.BaseDrugDep;
import com.ax.base.drugadministration.service.DrugAdministrationDepService;
import com.ax.base.system.domain.MenuDO;
@Service("DrugAdministrationService")
public class DrugAdministrationDepServiceImpl implements DrugAdministrationDepService {
	
	@Autowired
	BaseDrugDepMapper baseDrugDepMapper;
	
	/**
	 * 药监查询(模块:药监)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<BaseDrugDep> queryDrugAdministrationDepSer(Map<String, Object> params){
		List<BaseDrugDep> baseDrugDeps = baseDrugDepMapper.queryDrugAdministrationDepMapper(params);
		return baseDrugDeps;
	}

	/**
	 * 药监增加(模块:药监部门)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugDep baseDrugDep
	 */
	@Override
	public BaseDrugDep getBaseDrugDep(Long pId){
		BaseDrugDep baseDrugDep = baseDrugDepMapper.getDrugAdministrationDepMapper(pId);
		String[] dz = baseDrugDep.getAreaCode().split(",");
		baseDrugDep.setProvince(dz[0]);
		baseDrugDep.setCity(dz[1]);
		baseDrugDep.setVillage(dz[2]);
		return baseDrugDep;
	}
    
	/**
	 * 药监保存(模块:药监部门)
	 * 
	 * @param BaseDrugDep baseDrugDep
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertDrugDepSer(BaseDrugDep baseDrugDep) {
		baseDrugDep.setFirstLetter(PinYinUtil.getFirstSpell(baseDrugDep.getDrugDepName()));
		baseDrugDepMapper.insertSelective(baseDrugDep);
	}

	/**
	 * 药监修改(模块:药监部门)
	 * 
	 * @param BaseDrugDep baseDrugDep
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateDrugDepSer(BaseDrugDep baseDrugDep) {
		baseDrugDepMapper.updateByPrimaryKeySelective(baseDrugDep);
	}

	/**
	 * 药监状态修改(模块:药监部门)
	 * 
	 * @param String[] parameter,Integer flag
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateStateSer(String[] parameter,Integer flag) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("list",parameter);
		map.put("flag",flag);
		baseDrugDepMapper.updateStateMapper(map);
	}

	/**
	 * 树形列表(药监部门)
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> list() {
		List<Tree<MenuDO>> deps = baseDrugDepMapper.listDep();
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 编辑修改树
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> treeList(Long drugDepId) {
		String depId = drugDepId.toString();
		// 根据roleId查询权限
		List<Tree<MenuDO>> deps = baseDrugDepMapper.listDep();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (depId.equals(id)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}
	
	/**
	 * 名字是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean validateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String drugDepName = pt.substring(0,pt.indexOf(","));
		String parentDrugDepId = pt.substring(pt.indexOf(",") + 1);
		params.put("drugDepName",drugDepName);
		params.put("parentDrugDepId",parentDrugDepId);
		boolean validate;
		validate = baseDrugDepMapper.validateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 名字是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("drugDepName",pts[0]);
		params.put("parentDrugDepId",pts[1]);
		params.put("drugDepId",pts[2]);
		boolean validate;
		validate = baseDrugDepMapper.validateUpdateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * Tree
	 * 
	 * @param Long parentDrugDepId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getDrugTree(Long parentDrugDepId) {
		String drugDepId = parentDrugDepId.toString();
		// 根据roleId查询权限
		List<Tree<MenuDO>> deps = baseDrugDepMapper.listDrugDepId();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(drugDepId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

}
